import java.io.*;
import java.util.Random;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-07-19
 * Time:22:10
 * Text：
 */
public class Test1 {
     static Random random = new Random();
    //字节流读取
    public static void read(File file){
        try(FileInputStream fileInputStream = new FileInputStream(file)){
            int data;
            int num = 1;
            while((data = fileInputStream.read()) != -1){
                if(num%10 == 0){
                    System.out.println();
                    num = 1;
                }
                if((char)data != ' '){
                    System.out.print((char) data);
                    num++;
                }

            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //字节流写入
    public static void writer(File file){
        try(FileOutputStream fileOutputStream = new FileOutputStream(file,true)){
            String str = new String();
            for(int i = 0;i < 3;i++){
                str+=random.nextInt(10);
            }
            fileOutputStream.write(str.getBytes());
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //字符流写入
    public static void BufferWriter(File file){
        try(FileWriter fileOutputStream = new FileWriter(file,true)) {
            BufferedWriter bufferedWriter = new BufferedWriter(fileOutputStream);

            // 写入数据
            String data = "Hello,hh!";
            bufferedWriter.append(data);
            bufferedWriter.newLine(); // 写入换行符

            // 关闭流
            bufferedWriter.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void BufferRead(File file){
        try(FileReader reader = new FileReader(file)) {
            BufferedReader reader1 = new BufferedReader(reader);
            String str;
            while((str = reader1.readLine()) != null){
                System.out.println(str);
            }
        }  catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) throws IOException {
        File file = new File("./Test1.txt");
        BufferWriter(file);
        read(file);
        System.out.println("----------------");
        BufferRead(file);
    }
}
